//
//  XYMainTabBar.swift
//  01.XinLangWB
//
//  Created by xiaoyudong on 16/4/7.
//  Copyright © 2016年 苏小样. All rights reserved.
//

import UIKit

class XYMainTabBar: UITabBar{
     override init(frame: CGRect) {
       super.init(frame: frame)
        addSubview(pushBtn)
    }

     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }

    override func layoutSubviews(){
        super.layoutSubviews()
        let itemW = self.frame.width/5
        let itemH = self.bounds.height
        //按钮的大小
        let rect = CGRect (x: 0, y: 0, width: itemW, height: itemH)
        //遍历子视图
        var index : CGFloat = 0
        for subview in subviews{
            // print(subview)
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                //移动
                subview.frame = CGRectOffset(rect, index * itemW, 0)
                index += (index == 1) ? 2:1
                
                
                
            }
        }
        //添加加号按钮的frame
        pushBtn.frame = CGRectOffset(rect, 2*itemW, 0)
        
    }
    //懒加载加号
    lazy var pushBtn : UIButton = {
        let btn = UIButton()
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState:.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named:
            "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        return btn
    }()
  

}